package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivitySettingBinding
import com.play.ballen.home.ui.MainActivity
import com.play.ballen.me.dialog.LanguageSettingDialogUtils
import com.play.ballen.me.dialog.ModifyNickNameDialogUtils
import com.play.ballen.me.vm.SettingVM
import com.yariksoffice.lingver.Lingver
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.manager.AppActivitysManager
import com.yuven.appframework.manager.PackageInfoServer
import com.yuven.appframework.util.imageloader.ImageUtil
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil

class SettingActivity : BaseStdTitleActivity<SettingVM, ActivitySettingBinding>() {

    private val modifyNickNameDialogUtils by lazy {
        ModifyNickNameDialogUtils(mContext).apply {
            valuesCallback = {
                dataBinding.tvNickName.text = it
                viewModel.memberEdit(it)
            }
        }
    }

    private var currentCN = false
    private val languageSettingDialogUtils by lazy {
        LanguageSettingDialogUtils(this).apply {
            switchCallback = {
                restartToMain()
            }
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, SettingActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<SettingVM> {
        return SettingVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.memberIndexInfo.observe(this) {
            it.run {
                ImageUtil.instance.loadCircleImage(avatar, dataBinding.imgAvatar, 0, 0)
                dataBinding.tvNickName.text = nick_name
                dataBinding.tvEmail.text = email
            }
        }

        viewModel.actionLoginOutComplete.observe(this) {
            AppActivitysManager.getInstance().finishAllActivity()
            LoginActivity.start(this)
        }
    }

    override fun initData() {
        dataBinding.tvVersion.text = PackageInfoServer.getVersionName(this)
        viewModel.getMemberIndex()

        val localeListCompat = Lingver.getInstance().getLanguage()
        if (localeListCompat == KVConst.LANGUAGE_EN) {
            dataBinding.tvLanguage.text = ResourceUtil.getString(R.string.setting_language_english)
            currentCN = false
        } else {
            dataBinding.tvLanguage.text =
                ResourceUtil.getString(R.string.setting_language_traditional)
            currentCN = true
        }

        dataBinding.tvForgetLoginPwd.setOnSingleClickListener tag@{
            ForgetPasswordActivity.start(this, ForgetPasswordActivity.TYPE_LOGIN_PWD)
        }

        dataBinding.tvForgetPayPwd.setOnSingleClickListener tag@{
            ForgetPasswordActivity.start(this, ForgetPasswordActivity.TYPE_PAY_PWD)
        }

        dataBinding.rlNickName.setOnSingleClickListener tag@{
            modifyNickNameDialogUtils.show(dataBinding.tvNickName.text.toString())
        }

        dataBinding.rlLanguage.setOnSingleClickListener tag@{
            languageSettingDialogUtils.show(currentCN)
        }

        dataBinding.btnLogout.setOnSingleClickListener tag@{
            viewModel.logout()
        }
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun getToolBarTitle(): String = ResourceUtil.getString(R.string.setting_title)

    private fun restartToMain() {
        AppActivitysManager.getInstance().finishAllActivity()
        MainActivity.start(mContext)
    }
}